#include "Token.h"

std::string Token::generateRandomString(int length)
{
    const std::string characters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    std::random_device rd;
    std::mt19937 generator(rd());
    std::uniform_int_distribution<int> distribution(0, characters.size() - 1);

    std::string randomString;
    for (int i = 0; i < length; ++i)
    {
        randomString += characters[distribution(generator)];
    }

    return randomString;
}

Token::Token() {
    const int tokenLength = 16;

    do
    {
        token = generateRandomString(tokenLength);
    } while (tokenHash.find(token) != tokenHash.end());

    tokenHash[token] = true;
}

void Token::removeToken() {
    auto it = tokenHash.find(token);
    if (it != tokenHash.end())
    {
        tokenHash.erase(it);
        std::cout << "Token removed: " << token << std::endl;
    }
}

std::string Token::displayToken() {
    return token;
}

bool Token::isTokenExists(const std::string &tokenToCheck) {
    return tokenHash.find(tokenToCheck) != tokenHash.end();
}

void Token::deleteToken(const std::string &tokenToDelete) {
    auto it = tokenHash.find(tokenToDelete);
    if (it != tokenHash.end())
    {
        tokenHash.erase(it);
        std::cout << "Token deleted: " << tokenToDelete << std::endl;
    }
}
